package com.tjetc;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerMain {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器启动");
            //创建线程池
            ExecutorService pool = Executors.newFixedThreadPool(10);
            //循环接收客户端的请求
            while (true) {
                Socket socket = serverSocket.accept();
                pool.execute(new Runnable(){
                    @Override
                    public void run() {
                        try {
                            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                            //接口名
                            String interfaceName = in.readUTF();
                            //方法名
                            String methodName = in.readUTF();
                            //参数类型
                            Class<?>[] paramTypes = (Class<?>[]) in.readObject();
                            //参数
                            Object[] args = (Object[]) in.readObject();
                            //得到配置文件的输入流
                            InputStream inputStream = ServerMain.class.getClassLoader().getResourceAsStream("server" +
                                    ".properties");
                            //创建一个properties对象
                            Properties properties = new Properties();
                            //加载输入流
                            properties.load(inputStream);
                            //从properties对象取得接口实现类的全限定名
                            String implString = properties.getProperty(interfaceName);
                            Class<?> implClz = Class.forName(implString);
                            //通过反射创建对象
                            Object implInstance = implClz.getDeclaredConstructor().newInstance();
                            //通过反射调用方法
                            Method method = implClz.getDeclaredMethod(methodName, paramTypes);
                            //调用方法
                            Object result = method.invoke(implInstance, args);
                            //写回客户端
                            out.writeObject(result);
                            socket.close();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException(e);
                        } catch (InstantiationException e) {
                            throw new RuntimeException(e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        } catch (NoSuchMethodException e) {
                            throw new RuntimeException(e);
                        }

                    }
                });


            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
